package pers.corvey.studyjava.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.corvey.studyjava.dao.TestDAO;
import pers.corvey.studyjava.exception.BadRequestException;
import pers.corvey.studyjava.model.Test;

@Service
public class TestService {

    @Autowired
    private TestDAO testDAO;
    
    public Iterable<Test> getTests() {
        return testDAO.findAll();
    }
    
    public Test getTestById(String testId) {
        return testDAO.findOne(testId);
    }
    
    public Test createTest(Test test) {
        test.setGmtCreate(new Date());
        test.setGmtModified(new Date());
        return testDAO.save(test);
    }
    
    public Test updateTest(Test test) {
        if (!testDAO.exists(test.getId())) {
            throw new BadRequestException("操作失败，不存在相应的记录！");
        }
        test.setGmtModified(new Date());
        return testDAO.save(test);
    }
    
    public void deleteTestById(String testId) {
        testDAO.delete(testId);
    }
}
